package ca.camosun.bouncer;

import java.util.List;

import ca.camosun.bouncer.game.SingleScore;
import android.app.Activity;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
import android.nfc.NfcEvent;
import android.os.Bundle;
import android.os.Parcelable;
import android.widget.TextView;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;

public class HighScoreActivity extends Activity implements OnClickListener,
		CreateNdefMessageCallback {

	private NfcAdapter mNfcAdapter;
	private Button mainMenu;
	private TableLayout scoreTable;
	private TextView textView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_highscores);

		mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
		if (mNfcAdapter != null) {
			mNfcAdapter.setNdefPushMessageCallback(this, this);
		}

		TextView textView = (TextView) findViewById(R.id.textView1);
		textView.setText("NFC text should go here");

		scoreTable = (TableLayout) findViewById(R.id.tableLayout);

		mainMenu = (Button) findViewById(R.id.menuButton);
		mainMenu.setOnClickListener(this);

		displayScores();
	}

	private void displayScores() {
		Bouncer application = (Bouncer) getApplicationContext();
		List<SingleScore> scoreList = application.getHighScores();

		TableRow titleRow = new TableRow(this);
		TextView title = new TextView(this);

		if (scoreList.size() == 0) {
			title.setText("There are no scores yet!");
			titleRow.addView(title);
			titleRow.setGravity(Gravity.CENTER_HORIZONTAL);
			scoreTable.addView(titleRow);
			return;
		}

		title.setText("Your scores:");
		titleRow.addView(title);
		titleRow.setGravity(Gravity.CENTER_HORIZONTAL);
		scoreTable.addView(titleRow);

		for (SingleScore i : scoreList) {
			System.out.println("dan: score=" + i);

			TableRow row = new TableRow(this);

			TextView name = new TextView(this);
			TextView score = new TextView(this);

			name.setText(i.getName());
			score.setText(String.valueOf(i.getScore()));

			score.setGravity(Gravity.RIGHT);

			row.addView(name);
			row.addView(score);

			scoreTable.addView(row);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		if (v.equals(mainMenu)) {
			finish();

			Intent intent = new Intent(this, MainActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
		}
	}

	public NdefMessage createNdefMessage(NfcEvent arg0) {
		String text = "TEST";
		NdefMessage msg = new NdefMessage(
				new NdefRecord[] { NdefRecord.createMime(
						"application/ca.camosun.bouncer", text.getBytes())
				/**
				 * The Android Application Record (AAR) is commented out. When a
				 * device receives a push with an AAR in it, the application
				 * specified in the AAR is guaranteed to run. The AAR overrides
				 * the tag dispatch system. You can add it back in to guarantee
				 * that this activity starts when receiving a beamed message.
				 * For now, this code uses the tag dispatch system.
				 */
				// ,NdefRecord.createApplicationRecord("com.example.android.beam")
				});
		return msg;
	}

	@Override
	public void onResume() {
		super.onResume();
		// Check to see that the Activity started due to an Android Beam
		if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
			processIntent(getIntent());
		}
	}

	@Override
	public void onNewIntent(Intent intent) {
		// onResume gets called after this to handle the intent
		setIntent(intent);
	}

	void processIntent(Intent intent) {
		textView = (TextView) findViewById(R.id.textView1);
		Parcelable[] rawMsgs = intent
				.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
		// only one message sent during the beam
		NdefMessage msg = (NdefMessage) rawMsgs[0];
		// record 0 contains the MIME type, record 1 is the AAR, if present
		textView.setText(new String(msg.getRecords()[0].getPayload()));
	}

}

